const jwt = require('jsonwebtoken')

module.exports = () => {
    return async function auth(ctx, next) {
        let token = ctx.request.header.authorization;

        try {
            token = token.split('Bearer ')[1]
            jwt.verify(token, '1901A')
            await next()
        } catch (error) {
            ctx.body = {
                code: 2,
                msg: '登录失效'
            }
        }
    }
}